<template>
	<gui-page>
		<template v-slot:gBody>
			<gui-a-header title="徐州市残疾人就业服务平台" bg="transparent" color="#000"></gui-a-header>
			<view class="login flex-dc-ac">
				<image class="logo" src="../../static/logo.png" mode="widthFix" style="height: auto;"></image>
				<text class="f40 title gui-text-center">徐州市残疾人就业服务平台</text>
				<text class="tip">请确认一下授权信息</text>
				<text class="f24">获取你的信息（头像，信息等）</text>
				<view class="saveBtn gui-bold" @click="sumbit">微信授权</view>
			</view>
			<userInfoComponents ref="userInfoComponentsRef" @sumbit="loginHandle" @close="loginHandle"></userInfoComponents>
			<userType ref="userTypeRef" @confirm="confirm"></userType>
		</template>
	</gui-page>
</template>

<script setup>
	import userInfoComponents from '@/components/user-info/user-info.vue'
	import userType from '@/components/User-type/User-type';
	import {
		getWxCode,
		getUserProfile
	} from '@/utils/login'
	import {
		ref,
		reactive,
		inject
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	const $u = reactive(inject('$u'))
	import {
		authLogin,
		getBanner
	} from '@/api/app.js';
	import {
		getUserInfoByUser
	} from '@/api/user.js';

	const title = ref('登记成功')
	import {
		useUserStore
	} from '@/stores/user.js'
	const userInfoComponentsRef = ref(null)
	onLoad(() => {})
	const userTypeRef = ref(null);
	const user = ref({})
	const store = useUserStore()

	const confirm = (type) => {
		console.log(type.value);
		if (type.value == 1) { // 企业
			$u.to(`/pages/login/register`)
		} else if (type.value == 0) { // 用户
			getUserInfoByUser().then(res => {
				if (res.code == 200) {
					user.value.userType = 0;
					let form = Object.assign(user.value, {})
					store.setUser(form);
					userTypeRef.value.close();
					userInfoComponentsRef.value.handleOpen();
				}
			})
		}
	}

	const sumbit = async () => {
		const wxCode = await getWxCode()
		uni.showLoading({
			title: '请稍后...'
		})
		authLogin({
			code: wxCode
		}).then(res => {
			if (res.code == 200) {
				store.$patch({
					isLogin: true,
					token: res.data.token
				})
				user.value = res.data;
				store.setUser(res.data);
				if (res.data.userType == 2) {
					userTypeRef.value.open();
				} else {
					loginHandle();
				}
			}
		}).finally(() => {
			uni.hideLoading();
		})
	}

	const loginHandle = async () => {
		// #ifdef MP-WEIXIN  || APP-PLUS
		await store.getUserFun(true);
		if (user.value.userType == 1) {
			uni.switchTab({
				url: '/pages/index/index'
			})
		} else {
			uni.navigateBack();
		}
		//#endif
	}
</script>

<style lang="scss">
	.login {
		padding: 120rpx 70rpx 30rpx 70rpx;

		.logo {
			width: 160rpx;
			margin-bottom: 22rpx;
		}

		.title {
			width: 100%;
			padding-bottom: 50rpx;
			border-bottom: 1px solid #DEDCDF;
		}

		.tip {
			margin-top: 44rpx;
			margin-bottom: 36rpx;
		}

		.saveBtn {
			width: 100%;
			background: #00BF00;
			font-size: 32rpx;
			margin-top: 76rpx;
		}
	}
</style>